# -- coding: utf-8 --
"""
# MIAHX© 文件描述消息的 FastDDS 发布/订阅器

**模块：MIAHX© / 框架基础服务层（FFST） / 交互框架信息表示模型（IFIRM）**

Version: 0.4.10,
Last updated: October 20, 2024

## Contributor(s)
* YAO Naiming <[naiming@iscas.ac.cn](mailto:naiming@iscas.ac.cn)>

## Change log
### v0.4.10
- .20: [优化] Reader 构造方法传入 topic_name
- .2: [更新] 适配新接口
### v0.4.7
- .24: [优化] 更新 FileDescriptionWriter.write 方法实现
### v0.4.6
- .17: [优化] 去除 write 方法中的 frame 参数
### v0.4.5
- .23: [新增] 初始实现

---
Copyright © 2024-2025 MIAHX Contributors, ISCAS, and ASIC. All Rights Reserved.
Licensed under Apache-2.0.
"""

import miahx_fastdds as dds

from .participant import Participant
from .reader import Reader, ReaderListener
from .topics import Topics
from .writer import Writer


class FileDescriptionReader(Reader):
    def __init__(self, participant: Participant, on_data_listeners=None):
        topic_name = Topics.FILE_DESC
        topic_data_type = dds.FileDataFramePubSubType()
        super().__init__(participant, topic_name, topic_data_type,
                         listener=ReaderListener(topic_name, topic_data_type, on_data_listeners))


class FileDescriptionWriter(Writer):
    def __init__(self, participant: Participant):
        super().__init__(participant, Topics.FILE_DESC, dds.FileDataFramePubSubType())

    def write(self, file_name: str, file_size: int, file_sha: str, uts: float, iid: int):
        frame = dds.FileDataFrame()
        frame.header(self._create_frame_header(uts, iid))
        # body
        frame.file_name(file_name)
        frame.file_size(file_size)
        frame.file_sha(file_sha)
        # send
        self._writer.write(frame)
